package com.example.hotel.web;

import com.example.hotel.common.JsonResult;
import com.example.hotel.entity.Orders;
import com.example.hotel.service.OrdersService;
import com.example.hotel.vo.OrdersVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "获取订单信息")
@RestController
@CrossOrigin
@RequestMapping("order")
public class OrdersController {
    @Autowired
    private OrdersService service;
    //增加订单数量
    @ApiOperation(value = "增加订单")
    @GetMapping("addOrder")
    public JsonResult addOrder(Orders orders){
        try {
            int r = service.addOrder(orders);
            if (r > 0) {
                return JsonResult.ok();
            } else {
                return JsonResult.fail(5001, "订单添加失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail();}
    }
    @ApiOperation(value = "更新状态")
    @GetMapping("updateState")
    public JsonResult updateOrdersState(@ApiParam(value = "订单ID",example = "1") int orderId,@ApiParam(value = "订单状态",example = "已下单")String state){
        try {
            int r = service.updateOrdersState(orderId,state);
            if (r > 0) {
                return JsonResult.ok();
                } else {
                    return JsonResult.fail(5001, "订单状态更新失败");
                }
            }catch (Exception e) {
                e.printStackTrace();
                return JsonResult.fail();}
            }

   @ApiOperation(value = "查看所有")
   @GetMapping("selectAll")
   public JsonResult getAllOrders(){
       try {
           List<OrdersVO> list = service.getAllOrders();
           if(!list.isEmpty()){
               return JsonResult.ok(list);
           }else {
               return JsonResult.fail(5001, "订单查询失败");
           }
       }catch (Exception e) {
           e.printStackTrace();
           return JsonResult.fail();}
   }

    @ApiOperation(value = "删除订单")
    @DeleteMapping("deleteOrders")
    public JsonResult deleteOrders(@ApiParam(value = "订单ID",example = "1")int order_id){
        try {
            int r = service.deleteOrder(order_id);
            if (r > 0) {
                return JsonResult.ok();
            } else {
                return JsonResult.fail(5001, "订单删除失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail();}
    }



}
